package opera;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.ServletException;

import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.vo.ReportVO;

import com.jarp.db.DAO;
/**
 * 这个包里的类是为了执行一些特定外部操作，并不会加入到系统版本中
 * @author Administrator
 *
 */
public class AlterTable {

	DAO dao = new DAO();

	public AlterTable(){
	    String sql="SELECT * FROM TB_REPORT t WHERE t.REFFACT IS NOT NULL";
		int rows=0;
		ArrayList<String> SqlList=new ArrayList<String>(); //需要执行的所有SQL语句
		try {
			rows = dao.getDataSetCount("Jarp",sql);
			int pages=rows%1000==0?rows/1000:rows/1000+1;
			for(int i=1;i<=pages;i++){
			    Vector v=dao.getDataSet(ConstUtil.DBNAME, sql, i, 1000);
			    if(v!=null){
			    	for (int i2 = 0; i2 < v.size(); i2++) {
						Hashtable hashRows = (Hashtable) v.get(i2);
						ReportVO repVO = new ReportVO();
						repVO.setRepid((String) hashRows.get("REPID"));
						repVO.setRepcode((String) hashRows.get("REPCODE"));
						repVO.setRepname((String) hashRows.get("REPNAME"));
						repVO.setCataid((String) hashRows.get("CATANAME"));
						repVO.setRepdesc((String) hashRows.get("REPDESC"));
						repVO.setDesc((String) hashRows.get("CALDESC"));
						repVO.setDataprec((String) hashRows.get("DATAPREC"));
						repVO.setDataunit((String) hashRows.get("DATAUNIT"));
						repVO.setDepartmentid((String) hashRows.get("DEPARTMENT"));
						repVO.setDepartment((String) hashRows.get("DEPTNAME"));
						repVO.setSourcetype((String) hashRows.get("SOURCETYPE"));
						repVO.setDatatype((String) hashRows.get("DATATYPE"));
						repVO.setUpdatetime((String) hashRows.get("UPDATETIME"));
						repVO.setReffact((String) hashRows.get("REFFACT"));
						repVO.setRepvaluecol((String) hashRows.get("REPVALUECOL"));
	                    repVO.setIsselect((String)hashRows.get("ROLEID"));
	                    
	                    String alterSql="ALTER TABLE "+repVO.getReffact()+" ADD COLUMN version bigint default 1";
	                    SqlList.add(alterSql);
					}
			    }
			}
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(SqlList.size()>0){
			for(String SQL:SqlList){
				try {
					dao.execCommand(ConstUtil.DBNAME, SQL);
				} catch (ServletException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("添加完成！");
	}
}
